package com.yxjx.duoxue.dao;

import android.content.Context;

import com.yxjx.duoxue.Yx;
import com.yxjx.duoxue.global.Globals;
import com.yxjx.duoxue.network.UserLoginResponse;
import com.yxjx.duoxue.payment.ClientUserKidsDTS;
import com.yxjx.duoxue.utils.Utils;

/**
 * set方法，先写到内存，再写到sf或者db
 * get方法，先尝试取内存，（不存在，则先从sf或db写到内存）。
 * @author mengmengchen
 *
 */
public class Dao {
	
	/**
	 * save kidInfo
	 * @param ctx
	 * @param kidInfo
	 */
	public static void setUserKidInfo(Context ctx, ClientUserKidsDTS kidInfo) {
		Globals.getInstance(ctx).setKidInfo(kidInfo);
		Utils.writeToSharePreference(ctx, Yx.KEY_KID_INFO_STRING,
				kidInfo == null ? "" : kidInfo.toString());
	}
	
	/**
	 * get KidInfo
	 * @param ctx
	 * @return
	 */
	public static ClientUserKidsDTS getUserKidInfo(Context ctx) {
		ClientUserKidsDTS ret = Globals.getInstance(ctx).getKidInfo();
		if (ret == null) {
			ret = ClientUserKidsDTS.from(Utils.getValueFromSharedPreference(ctx, Yx.KEY_KID_INFO_STRING));
			if (ret != null) {
				setUserKidInfo(ctx, ret);
			}
		}
		return ret;
	}
	
	public static UserLoginResponse getUlRsp(Context ctx) {
		UserLoginResponse ulRsp = Globals.getInstance(ctx).getUlRsp();
		if (ulRsp == null) {
			ulRsp = UserLoginResponse.getInstance(ctx);
			if (ulRsp != null) {
				Globals.getInstance(ctx).setUlRsp(ulRsp);
			}
		}
		return ulRsp;
	}
	
	public static void setUlRsp(Context ctx, UserLoginResponse ulRsp) {
		Globals.getInstance(ctx).setUlRsp(ulRsp);
		Utils.writeUserLoginResponseToSharedPreference(ctx, ulRsp == null ? new UserLoginResponse() : ulRsp);
	}

}
